// export.js
const axios = require('axios');
const fs = require('fs');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;

// ===================
// 配置区（请修改）
// ===================
const RAGFLOW_URL = 'http://rf-dify.docker.internal:5175/v1';
const KB_ID = '28d7ab72ae4611f09fefc68f80c8256f';
const AUTH_TOKEN = 'IjVkMTA4YmIwYWVlMjExZjBhM2E3YzY4ZjgwYzgyNTZmIg.aPgscQ.zNpMSa7XE8CwD-lErNnefPGRNVw'; // 替换
const OUTPUT_FILE = `ragflow-export-${new Date().toISOString().split('T')[0]}.csv`;

// ===================
// 初始化 API 客户端
// ===================
const api = axios.create({
  baseURL: RAGFLOW_URL,
  headers: {
    'Authorization': AUTH_TOKEN,
    'Content-Type': 'application/json',
  },
  timeout: 10000,
});

// ===================
// CSV 写入器
// ===================
const csvWriter = createCsvWriter({
  path: OUTPUT_FILE,
  header: [
    { id: 'kb_id', title: '知识库ID' },
    { id: 'kb_name', title: '知识库名称' },
    { id: 'doc_id', title: '文档ID' },
    { id: 'doc_name', title: '文档名称' },
    { id: 'doc_status', title: '文档状态' },
    { id: 'chunk_count', title: '分块数量' },
    { id: 'create_time', title: '创建时间' },
    { id: 'update_time', title: '更新时间' },
  ],
});

// ===================
// 主函数
// ===================
async function main() {
  console.log('🚀 开始导出 RAGFlow 数据...');

  const records = [];

  try {
    // 1. 获取知识库列表
    const kbRes = await api.post('/kb/list', {page_size: 50, page: 1});
    const {kbs, total} = kbRes.data.data
    console.log(total)

    if (kbs.length === 0) {
      console.log('📭 没有找到知识库');
      return;
    }

    console.log(`✅ 获取到 ${kbs.length} 个知识库`);

    // 2. 遍历每个知识库，获取文档
    for (const kb of kbs) {
      console.log(`📁 处理知识库: ${kb.name} (${kb.id})`);

      const docRes = await api.post('/document/list', {
        params: { kb_id: kb.id, page_size: 50, page: 1 }
      });
      const docs = docRes.data.data || [];

      console.log(`📄 获取到 ${docs.length} 个文档`);

      // 3. 添加到记录
      docs.forEach(doc => {
        records.push({
          kb_id: kb.id,
          kb_name: kb.name,
          doc_id: doc.id,
          doc_name: doc.name,
          doc_status: doc.status,
          chunk_count: doc.chunk_num || 0,
          create_time: new Date(doc.create_time).toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' }),
          update_time: new Date(doc.update_time).toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' }),
        });
      });
    }

    // 4. 写入 CSV
    await csvWriter.writeRecords(records);
    console.log(`🎉 导出完成！数据已保存至: ${OUTPUT_FILE}`);
    console.log(`📊 共导出 ${records.length} 条记录`);

  } catch (error) {
    console.error('❌ 导出失败:', error.response?.data || error.message);
    process.exit(1);
  }
}

// 执行
main();
